package com.udi.project.business.base.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.udi.common.utils.StringUtils;
import com.udi.common.utils.security.ShiroUtils;
import com.udi.common.utils.text.Convert;
import com.udi.project.business.base.domain.BaseCorpVo;
import com.udi.project.business.base.mapper.BaseCorpMapper;
import com.udi.project.business.base.service.BaseCorpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseCorpServiceImpl implements BaseCorpService {

    @Autowired
    BaseCorpMapper baseCorpMapper;

    @Override
    public List<BaseCorpVo> selectcorplist(BaseCorpVo name) {
        return baseCorpMapper.selectcorplist(name);
    }

    @Override
    public BaseCorpVo selectCorpById(Integer id) {
        return baseCorpMapper.selectCorpById(id);
    }

    @Override
    public int insert_corp(BaseCorpVo baseCorpVo) {
        return baseCorpMapper.insert_corp(baseCorpVo);
    }

    @Override
    public int del_corp(String strs) {
        Integer[] ids = Convert.toIntArray(strs);
        for (Integer id: ids)
        {
            //做排除已关联操作
        }
        return baseCorpMapper.del_corp(ids);
    }

    @Override
    public int updateCorp(BaseCorpVo baseCorpVo) {
        return baseCorpMapper.edit_corp(baseCorpVo);
    }

    @Override
    public List<BaseCorpVo> selectzcbarlist(BaseCorpVo baseCorpVo) {
        return baseCorpMapper.selectzcbarlist(baseCorpVo);
    }

    @Override
    public BaseCorpVo selectZcbaById(Integer id) {
        return baseCorpMapper.selectZcbaById(id);
    }

    @Override
    public int insert_zcba(BaseCorpVo baseCorpVo) {
        return baseCorpMapper.insert_zcba(baseCorpVo);
    }

    @Override
    public int del_zcba(String ids) {
        Integer[] Ids = Convert.toIntArray(ids);

        return baseCorpMapper.del_zcba(Ids);
    }

    private Integer[] TurnTheStringArray(String str) {
        String[] strp=str.split(",");
        Integer[] ints = new Integer[strp.length];
        for(int i=0;i<strp.length;i++){
            ints[i] = Integer.parseInt(strp[i]);
        }
        System.out.println(ints);
        return ints;
    }


    @Override
    public int updateZcba(BaseCorpVo baseCorpVo) {
        return baseCorpMapper.edit_zcba(baseCorpVo);
    }


    @Override
    public List<BaseCorpVo> getdept(String id) {
        return baseCorpMapper.getdept(Integer.valueOf(id));
    }

    /**
     * 校验企业名称是否唯一
     *
     * @param baseCorpVo
     * @return 结果
     */
    @Override
    public String checkDeptNameUnique(BaseCorpVo baseCorpVo) {

        Integer Id = StringUtils.isNull(baseCorpVo.getId()) ? -1 : baseCorpVo.getId();
        BaseCorpVo info = baseCorpMapper.checkDeptNameUnique(Integer.parseInt(baseCorpVo.getDeptId()));
        if (StringUtils.isNotNull(info) && info.getId().intValue() != Id.intValue())
        {
            return "1";
        }
        return "0";
    }

    /**
     * 校验统一社会信用代码是否唯一
     *
     * @param baseCorpVo 岗位信息
     * @return 结果
     */
    @Override
    public String checkTyshxydmUnique(BaseCorpVo baseCorpVo) {
        Integer Id = StringUtils.isNull(baseCorpVo.getId()) ? -1 : baseCorpVo.getId();
        BaseCorpVo info = baseCorpMapper.checkTyshxydmUnique(baseCorpVo.getTyshxydm());
        if (StringUtils.isNotNull(info) && info.getId().intValue() != Id.intValue())
        {
            return "1";
        }
        return "0";
    }

    /**
     * 校验厂商码是否唯一
     *
     * @param baseCorpVo
     * @return 结果
     */
    @Override
    public String checkCorpCodeUnique(BaseCorpVo baseCorpVo) {
        Integer Id = StringUtils.isNull(baseCorpVo.getId()) ? -1 : baseCorpVo.getId();
        BaseCorpVo info = baseCorpMapper.checkCorpCodeUnique(baseCorpVo.getCorpCode());
        if (StringUtils.isNotNull(info) && info.getId().intValue() != Id.intValue())
        {
            return "1";
        }
        return "0";
    }
    /**
     * 校验注册备案号是否唯一
     * @param baseCorpVo
     * @return 结果
     */
    @Override
    public String checkZcbahUnique(BaseCorpVo baseCorpVo) {

        Integer Id = StringUtils.isNull(baseCorpVo.getId()) ? -1 : baseCorpVo.getId();
        //获取当前用户数据
        JSONObject user_arr = JSONObject.parseObject(JSONObject.toJSON(ShiroUtils.getSysUser()).toString());
        String current_deptId = ((JSONObject)user_arr.get("dept")).getString("deptId");
        System.out.println("-------------="+current_deptId+"-"+baseCorpVo.getZcbah());

        BaseCorpVo info = baseCorpMapper.checkZcbahUnique(Integer.parseInt(current_deptId),baseCorpVo.getZcbah());
        if (StringUtils.isNotNull(info) && info.getId().intValue() != Id.intValue()) {
            return "1";
        }
        return "0";
    }
    /**
     * 校验注册备案名称是否唯一
     * @param baseCorpVo
     * @return 结果
     */
    @Override
    public String checkZcbacpmcUnique(BaseCorpVo baseCorpVo) {
        Integer Id = StringUtils.isNull(baseCorpVo.getId()) ? -1 : baseCorpVo.getId();
        //获取当前用户数据
        JSONObject user_arr = JSONObject.parseObject(JSONObject.toJSON(ShiroUtils.getSysUser()).toString());
        String current_deptId = ((JSONObject)user_arr.get("dept")).getString("deptId");

        BaseCorpVo info = baseCorpMapper.checkZcbacpmcUnique(Integer.parseInt(current_deptId),baseCorpVo.getZcbacpmc());
        if (StringUtils.isNotNull(info) && info.getId().intValue() != Id.intValue()) {
            return "1";
        }
        return "0";
    }
}
